package cn.uexpo.lhz.controller.admin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.uexpo.lhz.plus.R;
import cn.uexpo.lhz.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.uexpo.lhz.biz.EventActivityBiz;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.utils.JsonData;


@RestController
@RequestMapping("/cert/eventActivity")
public class EventActivityController extends BaseController<EventActivity> {

    @Autowired
    private EventActivityBiz eventActivityBiz;


    /**
     * 根据ID删除单个活动信息
     *
     * @return
     */
    @GetMapping("/deleteActivitySingle")
    public R deleteActivitySingle(@RequestParam String activityId) {
        int result = eventActivityBiz.deleteById(Integer.parseInt(activityId));
        return R.ok().put("result", result);
    }

    /**
     * 根据ID修改单个活动信息
     *
     * @param eventActivity
     * @return
     */
    @PostMapping("/updateActivitySingle")
    public R updateActivitySingle(@RequestBody EventActivity eventActivity) {
        eventActivityBiz.updateByPrimaryKeySelective(eventActivity);
        return R.ok().put("eventActivity", eventActivity);
    }

    /**
     * 查询活动下的位置名称
     * 需值:活动ID  位置名称
     *
     * @param eventActivity
     * @return
     */
    @RequestMapping("/findLocationName")
    public Object findLocationName(@RequestBody EventActivity eventActivity) {
        eventActivityBiz.selectActivityIDAndLocationName(eventActivity);
        return JsonData.toJsonObject("查询位置名称", "200", true, eventActivity);
    }

    /**
     * 增加单个活动
     *
     * @param eventActivity
     * @return
     * @throws Exception
     */

    @PostMapping("/addEventActivitySingle")
    public Object addEventActivitySingle(@RequestBody EventActivity eventActivity) throws Exception {
        Date date = new Date();     //获取一个Date对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //创建一个格式化日期对象
        String time = simpleDateFormat.format(date);   //格式化后的时间
        Date times = simpleDateFormat.parse(time); //String-->Date
        eventActivity.setActivityCreatetime(times);
        eventActivity.setUntitled1("0");
        eventActivityBiz.insert(eventActivity);
        return JsonData.toJsonObject("项目活动新增成功", "200", true, eventActivity);
    }

    /**
     * 一对多查询
     * 根据项目ID查询 出对应活动
     * 活动和位置相对应
     *
     * @param
     * @return
     */
    @GetMapping("/findEventActivityAll2")
    public R findEventActivityAll2(@RequestParam String eventId) {
        List<EventActivity> list = eventActivityBiz.selectActivityAndLocation(eventId);
        return R.ok().put("list", list);
    }


    /**
     * 根据项目ID查询 出对应活动
     *
     * @param eventActivity
     * @return
     */
    @RequestMapping("/findEventActivityAll")
    public Object findEventActivityAll(@RequestBody EventActivity eventActivity) {
        EventActivityExample example = new EventActivityExample();
        EventActivityExample.Criteria criteria = example.createCriteria();
        if (eventActivity.getEventId() != 0) {
            criteria.andEventIdEqualTo(eventActivity.getEventId());
        }
        example.setOrderByClause("activity_createtime desc");
        List<EventActivity> list = eventActivityBiz.selectByExample(example);

        JsonData jsonData = new JsonData();
        jsonData.setCode(200);
        jsonData.setData(list);
        return jsonData;
    }
}
